package com.itheima.demo01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveMessage {
    public static void main(String[] args) throws IOException {
        //进行数据的接收

        //1.创建对象
        DatagramSocket ds =new DatagramSocket(10086);

        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);

        while (true) {
            //进行数据的接收
            ds.receive(dp);
            //获取数据
            byte[] data = dp.getData();
            int len = dp.getLength();
            String position = dp.getAddress().getHostAddress();
            String name = dp.getAddress().getHostName();
            System.out.println("ip为" + position + "主机名为" + name +"的人发送了数据"+ new String(data,0,len));
        }
    }
}
